fileupload = function ( dest_div, url, params, method )
{
	if ( typeof ( FormData ) == 'undefined' )
	{
		console.error ( 'XMLHttpRequest level 2 not found!' );
		return null;
	}
	this.dest_div = dest_div;
	this.url = url;
	this.method = method || "POST";
	this.params = params || {};
	this.files = [];
};

fileupload.prototype.from_input = function ( input )
{
	if ( ! input.type || input.type != 'file' ) return;
	for ( var i = 0; i < input.files.length; i++ )
	{
		this.files.push ( input.files.item ( i ) );
	}
};

fileupload.prototype.add_file = function ( file )
{
	if ( ! file || ! file.name || ! file.type || ! file.size ) return;
	this.files.push ( file );
};

fileupload.prototype.upload = function ()
{
	var fd = new FormData ();
	for ( var k in this.params )
	{
		var v = this.params [ k ];
		fd.append ( k, v );
	}
	for ( var i in this.files )
	{
		var f = this.files [ i ];
		fd.append ( 'file' + i, f );
	}
	var xhr = new XMLHttpRequest ();
	var upload = xhr.upload;
	var self = this;
	upload.addEventListener ( 'progress', function ( e ) { self._onprogress.call ( self, e ); }, false );
	upload.addEventListener ( 'load', function ( e ) { self._onload.call ( self, e ); }, false );
	upload.addEventListener ( 'error', function ( e ) { self._onerror.call ( self, e ); }, false );
	xhr.open ( this.method, this.url );
	xhr.send ( fd );
};

fileupload.prototype.reset = function ()
{
	this.files = [];
};

fileupload.prototype.debug = function ()
{
	for ( var i in this.files )
	{
		var f = this.files [ i ];
		console.log ( "File %d : %s %s %s", i, f.name, f.type, f.size );
	}
};

fileupload.prototype._onprogress = function ( e )
{
	console.info ( 'PROGRESS: %o', arguments );
	this.dest_div.innerHTML = Math.round ( ( e.loaded / e.total ) * 100 ) + "%";
};

fileupload.prototype._onload = function ( e )
{
	console.info ( 'LOAD: %o', arguments );
	this.dest_div.innerHTML = "LOADED!";
};

fileupload.prototype._onerror = function ( e )
{
	console.error ( e );
};
